﻿using System.Collections;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Entity;
using UnityEngine;

namespace Gameplay.PVE
{
    public class IsHaveLockTarget : Havior
    {
        
        private BattleUnit unit;
        public override ETaskResult Execute(ETaskResult result,bool last)
        {
            if (unit.Data.lockTarget != null)
            {
                if (unit.Data.lockTarget.Data.isDead || !unit.Data.lockTarget.Data.selectable || (unit.Data.searchType == 1 && unit.Data.lockTarget.Data.CurrentHp == unit.Data.lockTarget.Data.TotalHp))
                {
                    unit.Data.SetLockTarget(null);
                    unit.Data.SetTargetPosition(unit.Data.position + unit.Data.forward * 0.2f);
                    return ETaskResult.Successed;
                }
                else
                {
                    var replaceTarget = unit.Data.lockTarget.Data.GetReplaceObstacleUnit();
                    if (replaceTarget != null && replaceTarget.Data.team != unit.Data.team)
                    {
                        unit.Data.SetLockTarget(replaceTarget);
                        return ETaskResult.Failed;
                    }
                }
                /*if (unit.Data.searchType == 1) &&
                    unit.Data.lockTarget.Data.CurrentHp == unit.Data.lockTarget.Data.TotalHp)
                {
                    unit.Data.SetLockTarget(null);
                    unit.Data.SetTargetPosition(unit.Data.position);
                    return ETaskResult.Successed;
                }*/
                return ETaskResult.Failed;
            }
            return ETaskResult.Successed;
        }

        public override void Parse(Dictionary<string, object> data)
        {
            
        }

        public override void SharedVariableChanged(string key, object value)
        {
            if (key == ESharedVariablesType.Executor)
            {
                unit = (BattleUnit) value;
            }
        }
    }

}
